package com.atguigu.ware.wares.listener;
import com.atguigu.common.to.rabbit.OrderToWaresTo;
import com.atguigu.ware.wares.entity.WmsWareOrderTaskDetailEntity;
import com.atguigu.ware.wares.service.WmsWareSkuService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {


    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private WmsWareSkuService wmsWareSkuService;
    /**
     * 监听库存解锁方法--到时间自动触发
     */
    @RabbitHandler
    public void unlock(WmsWareOrderTaskDetailEntity entity, Message message, Channel channel) throws IOException {

        try {
            wmsWareSkuService.unlock(entity,message,channel);
            //在这手动ack处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
           //重新入队
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);

        }
    }
    /**
     * 监听库存解锁方法--订单发消息触发自动触发
     */
    @RabbitHandler
    public void orderToWaresTo(OrderToWaresTo entity, Message message, Channel channel) throws IOException {
        try {
            wmsWareSkuService.orderToWaresTo(entity,message,channel);
            //在这手动ack处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //重新入队
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
